﻿using System.Text;
using Newtonsoft.Json.Serialization;

namespace CRM.Json
{
    public class PascalPropertyNamesContractResolver : DefaultContractResolver
    {
        protected override string ResolvePropertyName(string propertyName)
        {
            return ChangeCase(propertyName);
        }

        private static string ChangeCase(string propertyName)
        {
            var sb = new StringBuilder();
            var isFirst = true;
            foreach (var c in propertyName)
            {
                if (!isFirst && char.IsUpper(c))
                {
                    sb.Append('_');
                }
                sb.Append(char.ToLower(c));
                isFirst = false;
            }

            return sb.ToString();
        }
        // private static string ChangeCase(string propertyName)
        // {
        //     var sb = new StringBuilder();
        //     var isNextUpper = true;
        //     foreach (var c in propertyName)
        //     {
        //         if (c == '_')
        //         {
        //             isNextUpper = true;
        //         }
        //         else
        //         {
        //             sb.Append(isNextUpper? char.ToUpper(c) : c);
        //             isNextUpper = false;
        //         }
        //     }
        //
        //     return sb.ToString();
        // }
    }
}